package com.example.zsf.activity.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.zsf.R;
import com.example.zsf.base.ZSFBaseActivity;
import com.example.zsf.model.MobileDetailModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ImageBrowserActivity extends ZSFBaseActivity {

    @BindView(R.id.viewPager)
    ViewPager2 viewPager;
    @BindView(R.id.textView72)
    TextView title_tv;
    @BindView(R.id.textView73)
    TextView pageProgress_tv;

    private List<MobileDetailModel.BannerImageInfo> bannerImageInfos;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browser);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        bannerImageInfos = getIntent().getParcelableArrayListExtra("banner_image_infos");
        index = getIntent().getIntExtra("index", 0);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this, R.layout.item_image_browser, bannerImageInfos);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                viewPager.post(new Runnable() {
                    @Override
                    public void run() {
                        MobileDetailModel.BannerImageInfo item = viewPagerAdapter.getItem(position);
                        if (!StringUtils.isEmpty(item.getTitle())) {
                            title_tv.setVisibility(View.VISIBLE);
                            title_tv.setText(item.getTitle());
                        } else {
                            title_tv.setVisibility(View.GONE);
                        }

                        pageProgress_tv.setText((position + 1) + "/" + bannerImageInfos.size());
                    }
                });
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
        viewPagerAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                finish();
            }
        });

        viewPager.setCurrentItem(index);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            intent.putExtra("index", viewPager.getCurrentItem());
            setResult(RESULT_OK, intent);
            finish();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private static class ViewPagerAdapter extends BaseQuickAdapter<MobileDetailModel.BannerImageInfo, BaseViewHolder> {

        private final Context context;

        public ViewPagerAdapter(Context context, int layoutResId, List<MobileDetailModel.BannerImageInfo> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, MobileDetailModel.BannerImageInfo bannerImageInfo) {
            ImageView view = (ImageView) baseViewHolder.getView(R.id.imageView12);
            Glide.with(context).load(bannerImageInfo.getImg_url()).placeholder(R.mipmap.loading).into(view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(ViewPagerAdapter.this, v, baseViewHolder.getAdapterPosition());
                    }
                }
            });
        }
    }
}